# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from openpyxl import Workbook
from datetime import datetime
import re

class ZhaopinPipeline(object):

    def __init__(self):
        self.wb = Workbook()
        self.ws = self.wb.active
        self.ws.append(['招聘职务', '公司名称', '招聘详情页链接', '薪资待遇', '工作地点'])
        self.savePath = '~/PycharmProjects/zhaopin/'
        self.rules = [
            u'法务',
            u'合规管理专员',
            u'法律',
            u'合规专员',
            u'风控专员',
            u'信用卡逾期专员',
            u'债权经理',
            u'资产运营助理',
            u'风控经理',
            u'合规主任',
            u'区域合规经理',
            u'企管中心律师',
            u'合约专员',
            u'合同管理',
            u'风险审核经理',
            u'诉讼专员',
            u'法律顾问',
            u'合规总监',
            u'风险合规部经理',
            u'合同管理员',
            u'律师主管',
            u'PPP项目法务顾问',
            u'合规主管',
            u'抵押专员'
        ]
        self.companys = [
            u'律师事务所',
            u'法律咨询',
            u'信息咨询'
        ]

    def process_item(self, item, spider):
        # 剔除不需要公司名
        result = 0
        for companyCell in self.companys:
            tempResult = re.findall(companyCell, item['company'])
            if len(tempResult) > 0:
                result += 1
                break
            else:
                result += 0
        if result > 0:
            return item

        # 进行职位过滤
        ruleResult = 0
        for ruleCell in self.rules:
            tempResult = re.findall(ruleCell, item['title'])
            if len(tempResult) > 0:
                ruleResult += 1
                break
            else:
                ruleResult += 0

        # 写入符合条件的职位，并剔除不符合条件的公司名
        if ruleResult > 0:
            line = [item['title'], item['company'], item['url'], item['sarlary'], item['site']]
            self.ws.append(line)
            fileSaveName = 'zhilian招聘信息_' + datetime.now().strftime('%Y%m%d_%H') + '.xlsx'
            self.wb.save(fileSaveName)
        return item


